/*
 * @(#)Mp3Player.java
 * 
 * Copyright(C) 2004 Yujin Kim
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.netexplode.jtunes.model.player;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.cdavies.itunes.request.NoServerPermissionException;

import com.netexplode.jtunes.client.player.jTunes;
import com.netexplode.jtunes.model.media.CollectionManager;
import com.netexplode.jtunes.model.media.ItunesFile;
import com.netexplode.jtunes.model.media.MediaFile;
import com.netexplode.jtunes.model.media.Mp3File;

/**
 * <code>Mp3Player</code> represents .....
 * 
 * @author ykim
 * @version $Revision: 1.5 $
 * @since 0.1
 */
public class Mp3Player extends ThreadedMediaPlayer {

    private static final Log log = LogFactory.getLog(Mp3Player.class);

    protected static Player player;

    protected synchronized void initPlayer(MediaFile mediaFile)
            throws PlayerException {
        if (player == null) {
            try {
                if (mediaFile instanceof Mp3File) {
                    player = new Player(
                            new FileInputStream(mediaFile.getFile()));
                }
                else if (mediaFile instanceof ItunesFile) {
                    ItunesFile file = (ItunesFile) mediaFile;
                    final String format = file.getTrack().getFormat();
                    log.info("format = " + file.getTrack().getFormat());
                    try {
                        if (format == null) {
                            //
                        }
                        if (format.equals("mp3")) {
                            player = new Player(file
                                    .getTrack()
                                    .getInputStream());
                        }
                        else {
                            // wrong player
                            player = new Player(file
                                    .getTrack()
                                    .getInputStream());
                        }

                    }
                    catch (NoServerPermissionException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                }
            }
            catch (FileNotFoundException e) {
                throw new PlayerException(e);
            }
            catch (JavaLayerException e) {
                throw new PlayerException(e);
            }
        }
    }

    public synchronized void play() throws PlayerException {
        this.play(CollectionManager.getInstance().getCurrentMediaFile());
    }

    public synchronized void play(MediaFile mediaFile) throws PlayerException {
        initPlayer(mediaFile);
        if (super.thread == null) {
            super.thread = new Thread(this, "Player Thread");
            initPlayer(mediaFile);
            super.thread.start();
        }
        PlayerManager.getInstance().setPlaying(true);
        jTunes.setLcdPanelText();

    }

    public synchronized void stop() throws PlayerException {
        if (player != null && !player.isComplete()) {
            player.close();
        }
        player = null;
        if (super.thread != null) {
            super.thread = null;
        }
        PlayerManager.getInstance().setPlaying(false);
    }

    /* THREAD HANDLING */
    public void run() {
        try {
            player.play();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}